package com.spe.visual.generator.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.spe.visual.generator.entity.Generator;
import com.spe.visual.generator.mapper.GeneratorMapper;
import com.spe.visual.generator.service.IGeneratorService;
import com.spe.visual.generator.vo.GeneratorVO;
import com.spe.visual.generator.vo.ProjectInfoVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author limingliang
 * @date 2020/12/12 16:43
 */
@Service
public class GeneratorServiceImpl extends ServiceImpl<GeneratorMapper, Generator> implements IGeneratorService {

    private static final String project = System.getProperty("user.dir");

    @Autowired
    GeneratorMapper generatorMapper;

    @Override
    public ProjectInfoVO getSubProject() {
        ProjectInfoVO projectInfoVO = new ProjectInfoVO();
        iterateFiles(projectInfoVO, new File(project));
        return projectInfoVO;
    }

    @Override
    public void generatorCode(GeneratorVO generatorVO) {

    }

    /**
     * 遍历父工程下所有子模块
     */
    private void iterateFiles(ProjectInfoVO projectInfo, File parentFile) {
        projectInfo.setProjectName(parentFile.getName()).setProjectPath(parentFile.getAbsolutePath());
        List<ProjectInfoVO> subProject = new ArrayList<>(6);
        Arrays.stream(parentFile.listFiles()).forEach(file -> {
            if (file.isDirectory() && file.getName().startsWith("spe-")) {
                ProjectInfoVO projectInfoVO = new ProjectInfoVO().setProjectName(file.getName()).setProjectPath(file.getAbsolutePath());
                subProject.add(projectInfoVO);
                iterateFiles(projectInfoVO, file);
            }
        });

        if (CollUtil.isNotEmpty(subProject)) {
            projectInfo.setIsService(false);
        } else {
            projectInfo.setIsService(true);
        }

        projectInfo.setSubProject(subProject);
    }

}
